package com.vancouver.knows.ui.activity.userpost;

import android.content.Context;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.refresh.ARefreshViewController;
import com.vancouver.knows.R;
import com.vancouver.knows.business.BSharedPrefManager;
import com.vancouver.knows.business.task.mark.PostPrivacySettingTaskMark;

/**
 * Created by Chris Li on 2016-07-28.
 */
public class FollowUserPostListsView extends UserPostListsView implements CompoundButton.OnCheckedChangeListener {

    private SwitchCompat mPrivacySwitch;

    public FollowUserPostListsView(Context context, ARefreshViewController mController) {
        super(context, mController);
    }

    @Override
    protected void addListHeader(ListView listView) {
        super.addListHeader(listView);
        RelativeLayout privacySwitchView = (RelativeLayout) mInflater.inflate(R.layout.view_privacy_switch, null, false);
        mPrivacySwitch = (SwitchCompat) privacySwitchView.findViewById(R.id.privacy_switch);
        String privacySetting = ((BSharedPrefManager) imContext.getSharedPrefManager()).getListingsPrivacySetting();
        if (privacySetting.equals("Y")) {
            mPrivacySwitch.setChecked(true);
        } else {
            mPrivacySwitch.setChecked(false);
        }
        mPrivacySwitch.setOnCheckedChangeListener(this);
        listView.addHeaderView(privacySwitchView);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String privacySetting;
        if (isChecked) {
            privacySetting = "Y";
        } else {
            privacySetting = "N";
        }

        PostPrivacySettingTaskMark taskMark = bModule.getTaskMarkPool().getPostPrivacySettingTaskMark();
        if (taskMark.getTaskStatus() == ATaskMark.HANDLE_DOING) {
            return;
        }

        String userId = ((BSharedPrefManager) imContext.getSharedPrefManager()).getUserId();
        bModule.getServiceWrapper().setListingsPrivacy(new IResultReceiver() {
            @Override
            public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
                if (exception == null) {
                    mPrivacySwitch.setOnCheckedChangeListener(null);
                    String privacySetting = ((BSharedPrefManager) imContext.getSharedPrefManager()).getListingsPrivacySetting();
                    if (privacySetting.equals("Y")) {
                        mPrivacySwitch.setChecked(true);
                    } else {
                        mPrivacySwitch.setChecked(false);
                    }
                    mPrivacySwitch.setOnCheckedChangeListener(FollowUserPostListsView.this);
                } else {
                    Toast.makeText(getContext(), exception.getExMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        }, taskMark, userId, privacySetting);
    }
}
